<template>
  <VPage>
    <VTable ref="tableRef" v-bind="tableOptions">
      <template #form="{ form }">
        <el-input v-model="form.code" class="w-40" placeholder="公司主体编码"></el-input>
        <el-input v-model="form.name" class="w-40" placeholder="公司主体名称"></el-input>
        <VSelect v-model="form.type" placeholder="公司类型" :options="options.type" multiple collapse-tags
          style="min-width: 160px" />
        <VSelect v-model="form.warehouse_ids" type="warehouse156" multiple collapse-tags placeholder="对应仓库"
          style="min-width: 160px" />
        <VGroup>
          <div class="v-group-item">更新时间</div>
          <VDate v-model="form.dateRange.value" />
        </VGroup>
      </template>
      <template #toolbar_btns>
        <VButton auth="/purchase/settings/company/add" type="primary" @click="handleRow('add')"><i class="dk-iconfont icon-Plus" />添加
        </VButton>
        <VButton auth="/purchase/settings/company/status" :loading="loadings.start" @click="handleBatch('start')">
          <VStatus :value="true" />
        </VButton>
        <VButton auth="/purchase/settings/company/status" :loading="loadings.stop" @click="handleBatch('stop')">
          <VStatus :value="false" />
        </VButton>
        <VExport auth='/purchase/settings/company/export' mainKey="company_id" name="company-list" :tableRef="tableRef"
          class="ml-auto" />
      </template>
    </VTable>
    <Add ref="addRef" :options="options" @success="query" />
    <VLog ref="logRef" />
  </VPage>
</template>
  
<script setup lang="jsx" name="Account">
import api from './api'
import Add from './components/Add'
import { getOption } from '@/api/public'
import { ElMessage } from 'element-plus'

const baseName = 'company'
const tableRef = ref()
const loadings = reactive({})
// 查询
const query = () => {
  tableRef?.value.query()
}

// 获取options
const options = ref({})
getOption({ type: baseName }).then(res => {
  options.value = res.data
})


// 批量操作
const handleBatch = (name) => {
  const selection = tableRef.value.$table.getCheckboxRecords()
  if (!selection.length) {
    ElMessage.warning('请先选择行')
    return
  }
  const company_ids = selection.map(d => d.company_id)
  const showLoading = () => loadings[name] = true
  const closeLoading = () => loadings[name] = false
  const handle = {
    start: () => {
      showLoading()
      api.status({ company_ids, status: 1 }).then(res => {
        query()
        ElMessage({ type: 'success', message: '操作成功!' })
      }).finally(closeLoading)
    },
    stop: () => {
      showLoading()
      api.status({ company_ids, status: 2 }).then(res => {
        query()
        ElMessage({ type: 'success', message: '操作成功!' })
      }).finally(closeLoading)
    },
  }
  handle[name]()
}

// 单行操作
const addRef = ref()
const logRef = ref()
const handleRow = (name, row = {}, rowIndex) => {
  const { company_id } = row
  const showLoading = () => {
    loadings[name + rowIndex] = true
  }
  const closeLoading = () => {
    loadings[name + rowIndex] = false
  }
  const handle = {
    add: () => {
      addRef?.value.open()
    },
    edit: () => {
      addRef?.value.open(company_id)
    },
    log: () => {
      logRef?.value.open({ log_type: baseName, company_id })
    },
  }
  handle[name]()
}

// 列表配置
const tableOptions = reactive({
  formConfig: {
    data: {
      account_name: '',
      purchase_account_id: [],
      account_type: [],
      dateRange: { type: 'update_time', value: [], range: true },
    }
  },
  columns: [
    { type: 'checkbox', fixed: 'left', width: 40 },
    { title: '编号', fixed: 'left', field: 'company_id', width: 60 },
    {
      field: 'code', title: '公司主体编码', minWidth: 180
    },
    {
      field: 'name', title: '公司主体名称', minWidth: 180
    },
    {
      field: 'type_name', title: '公司类型', width: 100
    },
    { field: 'address', title: '公司地址', width: 120 },
    {
      field: 'warehouse_name', title: '对应仓库', minWidth: 140, slots: {
        default: ({ row }) => <VText value={row.warehouse_name.join('，')} line={2} />
      }
    },
    {
      field: 'create_user', title: '添加人', minWidth: 120, slots: {
        default: ({ row }) => <VText value={row.create_user} />
      }
    },
    {
      field: 'update_time', title: '添加时间', width: 170, slots: {
        default: ({ row }) => <VText value={row.update_time} />
      }
    },
    {
      field: 'status', title: '状态', width: 70, slots: {
        default: ({ row }) => <VStatus value={row.status === 1} />
      }
    },
    {
      title: '操作', width: 110, fixed: 'right', slots: {
        default: ({ row, rowIndex }) => {
          return <div class='nm flex gap-2'>
            <VButton auth="/purchase/settings/company/edit" link type="primary" onClick={() => handleRow('edit', row)}>编辑</VButton>
            <VButton auth="/purchase/settings/company/log" link type="primary" onClick={() => handleRow('log', row)}>日志</VButton>
          </div>
        }
      }
    },
  ],
  proxyConfig: {
    autoLoad: false,
    ajax: {
      query: ({ page, form }) => api.query({ ...form, ...page })
    }
  }
})
</script>
  